﻿#region License
/*
 *CopyRight (c) By Denuvead All Rights Reserved
 *CreateDate:
 *Info:
 */
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace PriErp.Tools.Extensions
{
    /// <summary>
    /// 扩展类
    /// </summary>
    public static partial class PriErpExtension
    {
        /// <summary>
        /// 复制XElement
        /// </summary>
        /// <param name="el">源XElement</param>
        /// <returns>XElement</returns>
        static public XElement Copy(this XElement el)
        {
            return el == null ? null : new XElement(el);
        }

        /// <summary>
        /// 判断是是否为空(空则返回true)
        /// </summary>
        /// <param name="el"></param>
        /// <returns></returns>
        static public bool IsNullOrEmpty(this XElement el)
        {
            return el == null || el.IsEmpty;
        }

        /// <summary>
        /// 判断是否有子节点(空则返回false)
        /// </summary>
        /// <param name="el">需判断的XElement</param>
        /// <returns>返回是否含有Element</returns>
        static public bool NotNullOrEmpty(this XElement el)
        {
            return !el.IsNullOrEmpty();
        }

        /// <summary>
        /// 判断是否有值
        /// </summary>
        /// <param name="el">需判断的XElement</param>
        /// <returns></returns>
        static public bool HasValue(this XElement el)
        {
            return el != null && !string.IsNullOrEmpty(el.Value);
        }
        /// <summary>
        /// 判断是否有子Element(空则返回false)
        /// </summary>
        /// <param name="el">需判断的XElement</param>
        /// <param name="elementName">节点名称</param>
        /// <returns>返回是否含有Element</returns>
        static public bool HasElement(this XElement el, string elementName)
        {
            return el != null && el.Element(elementName) != null;
        }

        /// <summary>
        /// 比较XElement内容是否一致
        /// </summary>
        /// <param name="el">XElement</param>
        /// <param name="element">比较的XElement</param>
        /// <returns>返回是否一致</returns>
        static public bool EqualsEx(this XElement el, XElement element)
        {
            return el.ToStringOrEmpty()
                     .Equals(element.ToStringOrEmpty(),
                             StringComparison.OrdinalIgnoreCase);
        }

        /// <summary>
        /// 转换为String(类型为空时返回String.Empty)
        /// </summary>
        /// <param name="el">需要转换的XElement</param>
        /// <returns>返回String</returns>
        static public string ToStringOrEmpty(this XElement el)
        {
            return el.ToStringOrDefault(string.Empty);
        }

        /// <summary>
        /// 转换为String(类型为空时返回Null)
        /// </summary>
        /// <param name="el">需要转换的XElement</param>
        /// <returns>返回String</returns>
        static public string ToStringOrNull(this XElement el)
        {
            return el.ToStringOrDefault(null);
        }

        /// <summary>
        /// 转换为String(类型为空时返回默认值)
        /// </summary>
        /// <param name="el">需要转换的XElement</param>
        /// <param name="sVal">默认值</param>
        /// <returns>返回String</returns>
        static public string ToStringOrDefault(this XElement el, string sVal)
        {
            return el == null ? sVal : el.ToString();
        }

        /// <summary>
        /// 读取XElement的Value(XElement为空则返回String.Empty)
        /// </summary>
        /// <param name="el">读取的XElement</param>
        /// <returns>返回String</returns>
        static public string ValueOrEmpty(this XElement el)
        {
            return el.ValueOrDefault(string.Empty);
        }

        /// <summary>
        /// 读取XElement的Value(XElement为空则返回Null)
        /// </summary>
        /// <param name="el">读取的XElement</param>
        /// <returns>返回String</returns>
        static public string ValueOrNull(this XElement el)
        {
            return el.ValueOrDefault(null);
        }

        /// <summary>
        /// 读取XElement的Value(XElement为空则返回默认值)
        /// </summary>
        /// <param name="el">读取的XElement</param>
        /// <param name="sVal">默认值</param>
        /// <returns>返回String</returns>
        static public string ValueOrDefault(this XElement el, string sVal)
        {
            return el == null ? sVal : el.Value;
        }
    }
}
